﻿/**
 * @file service_data_pool.hpp
 * @date 2021/04/13 20:34
 *
 * @author harrybao (harrybao@123u.com)
 *
 * @brief
 *
 * @version v1.0.0
 *
 * @copyright Copyright © 2021 All Rights Reserved 上海欢乐互娱网络科技有限公司
 *
 * @par history:
 * <table>
 * <tr><th>Date                     <th>Version     <th>Author <th>Description
 * <tr><td>2021/04/13 20:34         <td>1.0.0       <td>harrybao
 * <td>创建初始版本
 * </table>
 */

// #ifndef SERVICE_DATA_POOL_HPP
// #define SERVICE_DATA_POOL_HPP

#pragma once

#include <vector>

namespace guild_service {
namespace detail {

using pool_index_type = std::size_t;  ///< 数据池索引下标类型

/**
 * @brief 服务数据池类
 *
 */
template <typename T,
          template <typename Elem, typename Alloc = std::allocator<Elem>>
          class Container = std::vector>
class service_data_pool final {
 public:
  service_data_pool(pool_index_type size = 64) : m_data_list(size) {}

  ~service_data_pool() = default;

  /**
   * @brief
   *
   * @param value
   */
  inline void push(const T &value) { m_data_list.emplace_back(value); }

  /**
   * @brief
   *
   * @param value
   */
  inline void push(T &&value) {
    m_data_list.emplace_back(std::forward<T>(value));
  }

  /**
   * @brief
   *
   * @param index
   * @return const T&
   */
  inline const T &get(pool_index_type index) const {
    if (index >= m_data_list.size()) {
      // return empty_data();
      resize(index * 2 + 1);
    }

    return m_data_list[index];
  }

  /**
   * @brief
   *
   * @param index
   * @return T&
   */
  inline T &get(pool_index_type index) {
    if (index >= m_data_list.size()) {
      // return empty_data();
      resize(index * 2 + 1);
    }

    return m_data_list[index];
  }

  /**
   * @brief
   *
   * @return T&
   */
  // inline
  // T& empty_data() {
  //     return m_empty_data;
  // }

  /**
   * @brief
   *
   * @param data
   * @return true
   * @return false
   */
  inline bool is_empty_data(const T &data) const {
    return false;
    // return data == m_empty_data;
  }

  /**
   * @brief
   *
   * @param index
   * @param value
   * @return true
   * @return false
   */
  inline bool set(pool_index_type index, const T &value) {
    if (index >= size()) {
      resize(index * 2 + 1);
    }

    m_data_list[index] = value;

    return true;
  }

  /**
   * @brief
   *
   * @return pool_index_type
   */
  inline pool_index_type size() const { return m_data_list.size(); }

  /**
   * @brief
   *
   * @param num
   */
  inline void resize(pool_index_type count) { m_data_list.resize(count); }

 private:
  ///
  Container<T> m_data_list;
};

}  // namespace detail
}  // namespace guild_service

// #endif  // SERVICE_DATA_POOL_HPP